package gdut.edu.demo04;

import java.util.Arrays;

/**
 * @Description 冒泡排序
 * @Author JiaFu
 * @Date 2022/9/18 14:04
 */
public class BubbleSort {
    public static void main(String[] args) {

        Integer[] arr = new Integer[]{2, 4, 3, 6, 2, 8, 9, 1, 4, 2, 7, 3};
        System.out.println(Arrays.toString(arr));
//        bubbleSort(arr);
        bubbleSort2(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void bubbleSort(Integer[] arr) {
        //共比较arr.length-1轮
        for (int i = arr.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }

    public static void bubbleSort2(Integer[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }
}
